define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () 
        {
            // 绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));

                if (panel.length > 0) 
                {
                    Controller.table[panel.attr("id")].call(this);

                    $(this).on('click', function (e) 
                    {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }

                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
            
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            business: function() 
            {
                Table.api.init({
                    extend: {
                        index_url: 'business/privatesea/recyclebin',
                        destroy_url: 'business/privatesea/destroy',
                        restore_url: 'business/privatesea/restore',
                        table: 'business',
                    }
                });

                var BusinessTable = $("#BusinessTable");
                
                BusinessTable.bootstrapTable({
                    url: $.fn.bootstrapTable.defaults.extend.index_url,
                    toolbar: "#BusinessToolbar", //工具栏
                    pk: 'id', //默认主键字段名
                    sortName: 'createtime', //排序的字段名
                    sortOrder: 'desc', //排序的方式
                    //渲染的数据部分
                    columns: [ //渲染的字段部分
                        { checkbox: true },
                        { field: 'id', title: __('ID'), sortable: true, operate: false },
                        { field: 'nickname', title: __('Bnickname'), operate: 'LIKE' },
                        { field: 'source.name', title: __('SName'), operate: 'LIKE' },
                        { field: 'gender_text', title: __('BsexText'), sortable: false, searchable: false },
                        { field: 'deal', title: __('BdealText'), searchList: { "0": __('未成交'), "1": __('已成交') }, formatter: Table.api.formatter.normal },
                        { field: 'auth', title: __('AuthStatus'), searchList: { "0": __('未认证'), "1": __('已认证') }, formatter: Table.api.formatter.normal }, 
                        {
                            field: 'deletetime', 
                            title: __('Deletetime'), 
                            operate: 'RANGE', 
                            addclass: 'datetimerange', 
                            sortable:true,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: "operate", 
                            title: __('Operate'),
                            table: BusinessTable,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'restore',
                                    title: '恢复',
                                    icon: 'fa fa-circle-o-notch',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax', 
                                    url: $.fn.bootstrapTable.defaults.extend.restore_url,
                                    extend:"data-toggle='tooltip'",
                                    confirm: "是否确认恢复数据",
                                    success: () => BusinessTable.bootstrapTable('refresh')
                                },
                                {
                                    name: 'destroy',
                                    title: '销毁',
                                    icon: 'fa fa-trash',
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax', 
                                    url: $.fn.bootstrapTable.defaults.extend.destroy_url,
                                    extend:"data-toggle='tooltip'",
                                    confirm: "是否确认真实销毁数据",
                                    success: () => BusinessTable.bootstrapTable('refresh')
                                },
                            ]
                        }
                    ]
                })

                // 为表格绑定事件
                Table.api.bindevent(BusinessTable);

                $(".btn-restore").on('click', function(){
                    //获取table勾选的id值
                    var ids = Table.api.selectedids(BusinessTable)

                    //弹框
                    layer.confirm(
                        '是否确认恢复数据', 
                        {title:"恢复提醒", btn: ['是', '否']},
                        function (index)
                        {
                            //发送ajax请求
                            Backend.api.ajax(
                                {url: $.fn.bootstrapTable.defaults.extend.restore_url+`?ids=${ids}`},
                                () => {
                                    //关闭弹框
                                    layer.close(index)

                                    //刷新表格
                                    BusinessTable.bootstrapTable('refresh')
                                }
                            )
                        }
                    )
                })

                $(".btn-destroy").on('click', function(){
                    //获取table勾选的id值
                    var ids = Table.api.selectedids(BusinessTable)

                    //弹框
                    layer.confirm(
                        '是否确认真实删除数据', 
                        {title:"真实删除标题", btn: ['是', '否']},
                        function (index)
                        {
                            //发送ajax请求
                            Backend.api.ajax(
                                {url: $.fn.bootstrapTable.defaults.extend.destroy_url+`?ids=${ids}`},
                                () => {
                                    //关闭弹框
                                    layer.close(index)

                                    //刷新表格
                                    BusinessTable.bootstrapTable('refresh')
                                }
                            )
                        }
                    )
                })
            },
        },
        api: {
            bindevent: function () 
            {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});